#!/bin/sh
# $Id: run_mpg123.sh,v 1.6 2009-03-27 08:23:30 brian Exp $
# Copyright (c)2009 by Brian Manning (elspicyjack at gmail dot com)
# PLEASE DO NOT E-MAIL THE AUTHOR ABOUT THIS SOFTWARE
# The proper venue for questions is the Propaganda mailing list at:
# http://groups.google.com/group/psas or <psas@groups.google.com>
#
# start mpg123 using the URL provided; start it in a while loop so if it dies,
# it can get restarted after a configurable delay

### MAIN SCRIPT ###
# what's my name?
SCRIPTNAME=$(basename $0)
# exit status
EXIT=0
# delay between restarts of mpg123
RESTART_DELAY=3
# our stream URL
STREAM_URL="http://localhost:8000/propaganda"
# PID of mpg123
MPG123_PID="/var/run/mpg123.pid"
# our PID
RUN_MPG123_PID="/var/run/run_mpg123.pid"

# set quiet mode by default, needs to be set prior to the getops call
QUIET=1

### SCRIPT SETUP ###
TEMP=$(/usr/bin/getopt -o hvd:u: \
    --long help,verbose,delay:,url: -n '${SCRIPTNAME}' -- "$@")

# if getopts exited with an error code, then exit the script
#if [ $? -ne 0 -o $# -eq 0 ] ; then
if [ $? != 0 ] ; then 
	echo "Run '${SCRIPTNAME} --help' to see script options" >&2 
	exit 1
fi

# Note the quotes around `$TEMP': they are essential!
# read in the $TEMP variable
eval set -- "$TEMP"

# read in command line options and set appropriate environment variables
# if you change the below switches to something else, make sure you change the
# getopts call(s) above
ERRORLOOP=1
while true ; do
	case "$1" in
		-h|--help) # show the script options
		cat <<-EOF

	${SCRIPTNAME} [options]

	SCRIPT OPTIONS
	-h|--help       Displays this help message
	-v|--verbose    Nice pretty output messages
    -d|--delay      Number of seconds between restarts of mpg123
    -u|--url        URL to connect to for streaming
    NOTE: Long switches do not work with BSD systems (GNU extension)

EOF
		exit 0;;		
		-q|--quiet)	# don't output anything (unless there's an error)
						QUIET=1
						shift;;
        -v|--verbose) # output pretty messages
                        QUIET=0
                        shift;;
        -d|--delay) # delay between runs of mpg123
                        MPG123_DELAY=$2; 
                        ERRORLOOP=$(($ERRORLOOP - 1));
                        shift 2;;
        -u|--url) # URL to connect to for streaming
                        STREAM_URL=$2; 
                        ERRORLOOP=$(($ERRORLOOP - 1));
                        shift 2;;
		--) shift; break;;
	esac
    # exit if we loop across getopts too many times
    ERRORLOOP=$(($ERRORLOOP + 1))
    if [ $ERRORLOOP -gt 4 ]; then
        echo "ERROR: too many getopts passes;"
        echo "Maybe you have a getopt option with no branch?"
        echo "Last option parsed was: ${1}"
        exit 1
    fi # if [ $ERROR_LOOP -gt 3 ];

done

# verify another copy of mpg123 is not running already
if [ -e $MPG123_PID ]; then
    exit 1
fi

# no, start a copy
while [ ! -e /tmp/mpg123.run ]; do
    echo $$ > $RUN_MPG123_PID
    # background mpg123, then keep checking for the PID in the output of PS;
    /usr/bin/mpg123 -o alsa $STREAM_URL >> /var/log/mpg123.log 2>&1 &
    echo $! > $MPG123_PID
    while [ -e $MPG123_PID ]; do
        PIDCHECK=$(/bin/cat $MPG123_PID | tr -d '\n')
        PSCHECK=$(/bin/ps | grep -c $PIDCHECK | tr -d '\n')
        if [ $PSCHECK -eq 0 ]; then
            /bin/rm $MPG123_PID
            break
        fi
        sleep $RESTART_DELAY
    done # while [ -e $MPG123_PID ]; do
done # while [ ! -e /tmp/mpg123.run ];

/bin/rm $MPG123_PID
/bin/rm $RUN_MPG123_PID
exit ${EXIT}

### BEGIN LICENCE
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 dated June, 1991.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
### END LICENSE

# vi: set ft=sh sw=4 ts=4:
# end of line
